<form class="rte_form{{ not is_ajax ? ' disableAjax' }}" action="{{ url('/database/routines') }}" method="post">
  <input name="{{ is_edit_mode ? 'edit_item' : 'add_item' }}" type="hidden" value="1">
  {% if is_edit_mode %}
    <input name="item_original_name" type="hidden" value="{{ routine.item_original_name }}">
    <input name="item_original_type" type="hidden" value="{{ routine.item_original_type }}">
  {% endif %}
  {{ get_hidden_inputs(db) }}

  <div class="card">
    <div class="card-header">
      {{ t('Details') }}
      {% if not is_edit_mode %}
        {{ show_mysql_docu('CREATE_PROCEDURE') }}
      {% endif %}
    </div>

    <div class="card-body">
      <table class="rte_table table table-borderless table-sm">
        <colgroup>
          <col>
          <col width="100%">
        </colgroup>

        <tr>
          <td class="text-nowrap">{{ t('Routine name') }}</td>
          <td>
            <input type="text" name="item_name" maxlength="64" value="{{ routine.item_name }}">
          </td>
        </tr>
        <tr>
          <td class="text-nowrap">{{ t('Type') }}</td>
          <td>
            {% if is_ajax %}
              <select name="item_type">
                <option value="PROCEDURE"{{ routine.item_type == 'PROCEDURE' ? ' selected' }}>PROCEDURE</option>
                <option value="FUNCTION"{{ routine.item_type == 'FUNCTION' ? ' selected' }}>FUNCTION</option>
              </select>
            {% else %}
              <input name="item_type" type="hidden" value="{{ routine.item_type }}">
              <div class="fw-bold text-center w-50">
                {{ routine['item_type'] }}
              </div>
              <input type="submit" class="btn btn-secondary" name="routine_changetype" value="{{ t('Change to %s')|format(routine.item_type_toggle) }}">
            {% endif %}
          </td>
        </tr>
        <tr>
          <td class="text-nowrap">{{ t('Parameters') }}</td>
          <td>
            <table class="routine_params_table table table-borderless table-sm">
              <thead>
                <tr>
                  <td></td>
                  <th class="routine_direction_cell{{ routine.item_type == 'FUNCTION' ? ' hide' }}">{{ t('Direction') }}</th>
                  <th>{{ t('Name') }}</th>
                  <th>{{ t('Type') }}</th>
                  <th>{{ t('Length/Values') }}</th>
                  <th colspan="2">{{ t('Options') }}</th>
                  <th class="routine_param_remove hide"></th>
                </tr>
              </thead>
              <tbody>
                {{ parameter_rows|raw }}
              </tbody>
            </table>
          </td>
        </tr>
        <tr>
          <td></td>
          <td>
            {% if is_ajax %}
              <button type="button" class="btn btn-primary" id="addRoutineParameterButton">{{ t('Add parameter') }}</button>
            {% else %}
              <input type="submit" class="btn btn-primary" name="routine_addparameter" value="{{ t('Add parameter') }}">
              <input type="submit" class="btn btn-secondary"  name="routine_removeparameter" value="{{ t('Remove last parameter') }}"{{ not routine.item_num_params ? ' disabled' }}>
            {% endif %}
          </td>
        </tr>
        <tr class="routine_return_row{{ routine.item_type == 'PROCEDURE' ? ' hide' }}">
          <td class="text-nowrap">{{ t('Return type') }}</td>
          <td>
            <select name="item_returntype">
              {{ get_supported_datatypes(routine.item_returntype) }}
            </select>
          </td>
        </tr>
        <tr class="routine_return_row{{ routine.item_type == 'PROCEDURE' ? ' hide' }}">
          <td class="text-nowrap">{{ t('Return length/values') }}</td>
          <td>
            <input type="text" name="item_returnlength" value="{{ routine.item_returnlength }}">
          </td>
          <td class="hide no_len">---</td>
        </tr>
        <tr class="routine_return_row{{ routine.item_type == 'PROCEDURE' ? ' hide' }}">
          <td class="text-nowrap">{{ t('Return options') }}</td>
          <td>
            <div>
              <select lang="en" dir="ltr" name="item_returnopts_text">
                <option value="">{{ t('Charset') }}</option>
                <option value=""></option>
                {% for charset in charsets %}
                  <option value="{{ charset.getName() }}" title="{{ charset.getDescription() }}"{{ routine.item_returnopts_text == charset.getName() ? ' selected' }}>{{ charset.getName() }}</option>
                {% endfor %}
              </select>
            </div>
            <div>
              <select name="item_returnopts_num">
                <option value=""></option>
                {% for numeric_option in numeric_options %}
                  <option value="{{ numeric_option }}"{{ routine.item_returnopts_num == numeric_option ? ' selected' }}>{{ numeric_option }}</option>
                {% endfor %}
              </select>
            </div>
            <div class="hide no_opts">---</div>
          </td>
        </tr>
        <tr>
          <td class="text-nowrap">{{ t('Definition') }}</td>
          <td>
            <textarea name="item_definition" rows="15" cols="40">{{ routine.item_definition }}</textarea><br>
            <input type="button" value="{{ t('Format') }}" id="format" class="btn btn-secondary button sqlbutton">
          </td>
        </tr>
        <tr>
          <td class="text-nowrap">{{ t('Is deterministic') }}</td>
          <td>
            <input type="checkbox" name="item_isdeterministic"{{ routine.item_isdeterministic|raw }}>
          </td>
        </tr>

        {% if is_edit_mode %}
          <tr>
            <td class="text-nowrap">
              {{ t('Adjust privileges') }}
              {{ show_docu('faq', 'faq6-39') }}
            </td>
            <td>
              {% if has_privileges %}
                <input type="checkbox" name="item_adjust_privileges" value="1" checked>
              {% else %}
                <input type="checkbox" name="item_adjust_privileges" value="1" title="{{ t('You do not have sufficient privileges to perform this operation; Please refer to the documentation for more details.') }}" disabled>
              {% endif %}
            </td>
          </tr>
        {% endif %}

        <tr>
          <td class="text-nowrap">{{ t('Definer') }}</td>
          <td>
            <input type="text" name="item_definer" value="{{ routine.item_definer }}">
          </td>
        </tr>
        <tr>
          <td class="text-nowrap">{{ t('Security type') }}</td>
          <td>
            <select name="item_securitytype">
              <option value="DEFINER"{{ routine.item_securitytype_definer|raw }}>DEFINER</option>
              <option value="INVOKER"{{ routine.item_securitytype_invoker|raw }}>INVOKER</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="text-nowrap">{{ t('SQL data access') }}</td>
          <td>
            <select name="item_sqldataaccess">
              {% for value in sql_data_access %}
                <option value="{{ value }}"{{ routine.item_sqldataaccess == value ? ' selected' }}>{{ value }}</option>
              {% endfor %}
            </select>
          </td>
        </tr>
        <tr>
          <td class="text-nowrap">{{ t('Comment') }}</td>
          <td>
            <input type="text" name="item_comment" value="{{ routine.item_comment }}">
          </td>
        </tr>
      </table>
    </div>

    {% if is_ajax %}
      <input type="hidden" name="{{ is_edit_mode ? 'editor_process_edit' : 'editor_process_add' }}" value="true">
      <input type="hidden" name="ajax_request" value="true">
    {% else %}
      <div class="card-footer">
        <input class="btn btn-primary" type="submit" name="{{ is_edit_mode ? 'editor_process_edit' : 'editor_process_add' }}" value="{{ t('Go') }}">
      </div>
    {% endif %}
  </div>
</form>
